﻿using Entity;
using Microsoft.AspNetCore.Http; 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ServiceCore.Token
{
    public interface ITokenUser_Service
    {
        rk_user USER { get; }


        TokenUser RK_USER { get; }

    }
    public class TokenUser_Service : ITokenUser_Service
    {
        private readonly IHttpContextAccessor httpcontext;
        public TokenUser_Service(IHttpContextAccessor HttpContext)
        {
            httpcontext = HttpContext;
        }
        public rk_user USER
        {
            get
            {
                try
                {
                    string token = "";
                    rk_user tokenUser = null;
                    if (httpcontext.HttpContext.Request.Headers["token"].FirstOrDefault() != null)
                    {
                        token = httpcontext.HttpContext.Request.Headers["token"].FirstOrDefault();
                    }
                    else if (httpcontext.HttpContext.Request.Query["token"].FirstOrDefault() != null)
                    {
                        token = httpcontext.HttpContext.Request.Query["token"].ToString();
                    }
                    if (!string.IsNullOrWhiteSpace(token))
                    {
                        try
                        {
                            var dic = JWT.Jwt.GetPayLoad(token); 
                            tokenUser = Newtonsoft.Json.JsonConvert.DeserializeObject<rk_user>(Newtonsoft.Json.JsonConvert.SerializeObject(dic["tokenuser"]));
                             
                        }
                        catch
                        {

                        }
                    }
                    return tokenUser;
                }
                catch
                {
                    return null;
                }
            }
            set
            {

            }
        }

        public TokenUser RK_USER
        {
            get
            {
                try
                {
                    string token = "";
                    TokenUser tokenUser = null;
                    if (httpcontext.HttpContext.Request.Headers["token"].FirstOrDefault() != null)
                    {
                        token = httpcontext.HttpContext.Request.Headers["token"].FirstOrDefault();
                    }
                    else if (httpcontext.HttpContext.Request.Query["token"].FirstOrDefault() != null)
                    {
                        token = httpcontext.HttpContext.Request.Query["token"].ToString();
                    }
                    if (!string.IsNullOrWhiteSpace(token))
                    {
                        try
                        {
                            var dic = JWT.Jwt.GetPayLoad(token);
                            tokenUser = Newtonsoft.Json.JsonConvert.DeserializeObject<TokenUser>(Newtonsoft.Json.JsonConvert.SerializeObject(dic));
                        }
                        catch
                        {

                        }
                    }
                    return tokenUser;
                }
                catch
                {
                    return null;
                }
            }
            set
            {

            }
        }
    }
}
